package vue;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.xml.ws.handler.MessageContext.Scope;

import vue.composants.Boutton;

import lib.Element;
import lib.Ensemble;
import lib.EnsembleListe;
import lib.EnsembleTableau;

public class AfficheEnsembleDialog extends JDialog implements ActionListener{

	private FenetrePrincipale mere;
	private Boutton bouttonAfficher;
	private JComboBox EnsembleCombo;
	private JLabel error;
	
	public AfficheEnsembleDialog(FenetrePrincipale mere)
	{
		super(mere);
		this.setMere(mere);
		this.setBounds(500, 500, 500, 500);
		
		if(!this.mere.getMesEnsemble().isEmpty())
		{

			this.bouttonAfficher = new Boutton("Afficher");
			this.bouttonAfficher.addActionListener(this);	
			
			this.EnsembleCombo = new JComboBox(this.getMere().getMesEnsemble().toArray());
			this.getContentPane().setLayout(new GridLayout());
			this.getContentPane().add(this.EnsembleCombo);
			this.getContentPane().add(this.bouttonAfficher);
		}
		else
		{
			this.error = new JLabel("Aucun ensemble disponible");
			this.getContentPane().add(this.error);
		}
		
		this.pack();
		this.setVisible(true);
	}

	/**
	 * @return the mere
	 */
	public FenetrePrincipale getMere() {
		return mere;
	}

	/**
	 * @param mere the mere to set
	 */
	public void setMere(FenetrePrincipale mere) {
		this.mere = mere;
	}
	
	private void afficheEnsemble()
	{
		Ensemble e = (Ensemble)this.EnsembleCombo.getSelectedItem();
		
		Object[][] elements = new Object[e.getCardinal()][e.getCardinal()];
		Object[] el = e.getElements().toArray();
		
		int i = 0;
		for(Element element : e.getElements())
		{
			elements[i++][0] = element.getValeur(); 
			//System.out.println(element.getValeur()+" "+elements[i-1][0]);
			//elements[0][i++] = element.getValeur();
		}
		
	      
	      String[] col = {"Valeur"};
	      
	      JTable table = new JTable(elements,(Object[])col);
	      JScrollPane scrollpane = new JScrollPane(table);
	      	      
	      
	      JPanel tmp =  new JPanel();
	      scrollpane.setFocusable(false);
	      tmp.add(scrollpane);
	      tmp.setSize(300,150);	  
	      tmp.setFocusable(false);
	      this.getMere().setPanEnsemble(tmp);
	      this.getMere().repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.bouttonAfficher)
		{
			this.afficheEnsemble();
		}
	}
}
